'''
https://leetcode.cn/problems/find-all-numbers-disappeared-in-an-array/
'''
from typing import List


class Solution:

    def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
        n = len(nums)
        i = 0
        while i < n:
            if i + 1 != nums[i]:
                temp = nums[i] - 1
                if nums[temp] != temp + 1:
                    nums[i], nums[temp] = nums[temp], nums[i]
                else:
                    i += 1
            else:
                i += 1
        res = []
        for i in range(n):
            if i + 1 != nums[i]:
                res.append(i + 1)
        return res

    def findDisappearedNumbers2(self, nums: List[int]) -> List[int]:
        n = len(nums) + 1
        nums = nums + [0]
        for i in range(1, n):
            while i != nums[i] and nums[nums[i]] != nums[i]:
                nums[nums[i]], nums[i] =  nums[i], nums[nums[i]]      # !!!
        res = []
        for i in range(1, n):
            if i != nums[i]:
                res.append(i)
        return res

    def findDisappearedNumbers3(self, nums: List[int]) -> List[int]:
        n = len(nums)
        seem = [False] * (n + 1)
        for num in nums:
            seem[num] = True
        return [i for i in range(1, n + 1) if not seem[i]]

nums = [4,3,2,7,8,2,3,1]
print(Solution().findDisappearedNumbers2(nums))